package Quotation;
import java.util.List;

import Log.FastExLog;
import bean.QuotationBean;

import com.jd.testjsf.HttpUtil;

import db.DbManagerDao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.QuotationToRedisService;
import service.QuotationService;
import service.QuotationServiceImpl;
import utils.DtoToBeanUtils;
import utils.PropertiesUtil;
import utils.TradeConst;
import utils.TradeTimeUtils;
/** 
 * @author yanchuanfeng
 * @version 创建时间：2022年2月24日 下午4:21:52 
 *  
 */

public class QuotationThread extends Thread{
	private static Logger log = LoggerFactory.getLogger(QuotationThread.class);
	
	public StockInfo getStockInfo(String stk){
		String url = "http://qt.gtimg.cn/q=s_" + stk;
		String quotaStr = HttpUtil.httpGet(url);
		StockInfo stockInfo = new StockInfo();
	    log.debug(quotaStr);
	    System.out.println(quotaStr);
	    String [] quota = quotaStr.split("=");
	    String[] stockInfoArray = quota[1].replaceAll("\"", "").split("~");
	    String exchId = stockInfoArray[0];
	    stockInfo.setExchId(exchId);
	    String stkName = stockInfoArray[1];
	    stockInfo.setStkName(stkName);
	    String stkId = stockInfoArray[2];
	    stockInfo.setStkId(stkId);
	    String newPrice = stockInfoArray[3];
	    stockInfo.setNewPrice(newPrice);
	    String LimitDown = stockInfoArray[4];
	    stockInfo.setLimitDown(LimitDown);
	    String LimitUp = stockInfoArray[5];
	    stockInfo.setLimitUp(LimitUp);
	    String knockQty = stockInfoArray[6];
	    stockInfo.setKnockQty(knockQty);
	    String knockAmt = stockInfoArray[7];
	    stockInfo.setKnockAmt(knockAmt);
	    String info = stockInfoArray[8];
	    stockInfo.setInfo(info);
	    String stkValue = stockInfoArray[9];
	    stockInfo.setStkValue(stkValue);
	    FastExLog.log(stockInfo.toString());	
	    return stockInfo;
	}
	/**
	 * 序号	返回值	含义
		1	1	代表交易所，200-美股（us），100-港股（hk），51-深圳（sz），1-上海（sh）
		2	贵州茅台	股票名字
		3	600519	股票代码
		4	2076.91	当前价格
		5	2033.00	昨收
		6	2000.00	开盘
		7	39811	成交量
		8	20166	外盘
		9	19646	内盘
		10	2076.03	买一
		11	8	买一量
		12	2076.02	买二
		13	6	买二量
		14	2076.01	买三
		15	2	买三量
		16	2076.00	买四
		17	7	买四量
		18	2075.90	买五
		19	2	买五量
		20	2076.05	卖一
		21	3	卖一量
		22	2076.82	卖二
		23	1	卖二量
		24	2076.91	卖三
		25	1	买三量
		26	2076.97	卖四
		27	1	卖四量
		28	0	卖五
		29	0	卖五量
		30	20210305110527	请求时间
		31	43.91	涨跌
		32	2.16	涨跌%
		33	2088.00	最高
		34	1988.00	最低
		35	2076.91/39811/8132289410	最新价/成交量（手）/成交额（元）
		36	39811	成交量
		37	813229	成交额
		38	0.32	换手率
		39	58.53	ttm市盈率
		40	-	-
		41	2088.00	最高
		42	1988.00	最低
		43	4.92	振幅
		44	26090.10	流通市值
		45	26090.10	总市值
		46	17.57	lf市净率
		47	2236.30	涨停价
		48	1829.70	跌停价
		49	1.65	量比
		50	9	-
		51	2042.72	均价
		52	57.85	动态市盈率
		53	63.32	静态市盈率
		54	-	-
		55	-	-
		56	1.09	-
		57	813228.9410	成交额
		58	0.0000	
		59	0	
		60	-	
		61	GP-A	
		62	3.95	
		63	-	
		64	0.82	
		65	30.03	
		66	25.68	
	 * @param stk
	 * @return
	 */
	public Quotation getQuotaInfo(String stk){
		String url = "http://qt.gtimg.cn/q=" + stk;
		String quotaStr = HttpUtil.httpGet(url);
		Quotation quotation = new Quotation();
	    log.debug(quotaStr);
	    System.out.println(quotaStr);
	    String [] quota = quotaStr.split("=");
	    String[] quotaInfoArray = quota[1].replaceAll("\"", "").split("~");
	    //取字段
	    String exchId = quotaInfoArray[0];//代表交易所，200-美股（us），100-港股（hk），51-深圳（sz），1-上海（sh）
	    quotation.setExchId(exchId);
	    String stkName = quotaInfoArray[1];//股票名字
	    quotation.setStkName(stkName);
	    String stkId = quotaInfoArray[2];//股票代码
	    quotation.setStkId(stkId);
	    String newPrice = quotaInfoArray[3];//当前价格
	    quotation.setNewPrice(newPrice);
	    String prevClosePrice = quotaInfoArray[4];//昨收
	    quotation.setPrevClosePrice(prevClosePrice);
	    String openPrice = quotaInfoArray[5];//开盘
	    quotation.setOpenPrice(openPrice);
	    String volume = quotaInfoArray[6];//成交量
	    quotation.setVolume(volume);
	    String sell = quotaInfoArray[7];//外盘
	    quotation.setSell(sell);
	    String buy = quotaInfoArray[8];//内盘
	    quotation.setBuy(buy);
		double buyPrice1 = Double.parseDouble(quotaInfoArray[9]);//买1
		quotation.setBuyPrice1(buyPrice1);
		long buyQty1 = Long.parseLong(quotaInfoArray[10]);//买1量
		quotation.setBuyQty1(buyQty1);
		double buyPrice2 = Double.parseDouble(quotaInfoArray[11]);//买2
		quotation.setBuyPrice2(buyPrice2);
		long buyQty2 = Long.parseLong(quotaInfoArray[12]);//买2量
		quotation.setBuyQty2(buyQty2);
		double buyPrice3 = Double.parseDouble(quotaInfoArray[13]);//买3
		quotation.setBuyPrice3(buyPrice3);
		long buyQty3 = Long.parseLong(quotaInfoArray[14]);//买3量
		quotation.setBuyQty3(buyQty3);
		double buyPrice4 = Double.parseDouble(quotaInfoArray[15]);//买4
		quotation.setBuyPrice4(buyPrice4);
		long buyQty4 = Long.parseLong(quotaInfoArray[16]);//买4量
		quotation.setBuyQty4(buyQty4);
		double buyPrice5 = Double.parseDouble(quotaInfoArray[17]);//买5
		quotation.setBuyPrice5(buyPrice5);
		long buyQty5 = Long.parseLong(quotaInfoArray[18]);//买5量
		quotation.setBuyQty5(buyQty5);
//		protected double buyPrice6 = quotaInfoArray[];
//		protected long buyQty6 = quotaInfoArray[];
//		protected double buyPrice7 = quotaInfoArray[];
//		protected long buyQty7 = quotaInfoArray[];
//		protected double buyPrice8 = quotaInfoArray[];
//		protected long buyQty8 = quotaInfoArray[];
//		protected double buyPrice9 = quotaInfoArray[];
//		protected long buyQty9 = quotaInfoArray[];
//		protected double buyPrice10 = quotaInfoArray[];
//		protected long buyQty10 = quotaInfoArray[];
		double sellPrice1 = Double.parseDouble(quotaInfoArray[19]);//卖1
		quotation.setSellPrice1(sellPrice1);
		long sellQty1 = Long.parseLong(quotaInfoArray[20]);//卖1量
		quotation.setSellQty1(sellQty1);
		double sellPrice2 = Double.parseDouble(quotaInfoArray[21]);//卖2
		quotation.setSellPrice2(sellPrice2);
		long sellQty2 = Long.parseLong(quotaInfoArray[22]);//卖2量
		quotation.setSellQty2(sellQty2);
		double sellPrice3 = Double.parseDouble(quotaInfoArray[23]);//卖3
		quotation.setSellPrice3(sellPrice3);
		long sellQty3 = Long.parseLong(quotaInfoArray[24]);//卖3量
		quotation.setSellQty3(sellQty3);
		double sellPrice4 = Double.parseDouble(quotaInfoArray[25]);//卖4
		quotation.setSellPrice4(sellPrice4);
		long sellQty4 = Long.parseLong(quotaInfoArray[26]);//卖4量
		quotation.setSellQty4(sellQty4);
		double sellPrice5 = Double.parseDouble(quotaInfoArray[27]);//卖5
		quotation.setSellPrice5(sellPrice5);
		long sellQty5 = Long.parseLong(quotaInfoArray[28]);//卖5量
		quotation.setSellQty5(sellQty5);
//		protected double sellPrice6 = quotaInfoArray[];
//		protected long sellQty6 = quotaInfoArray[];
//		protected double sellPrice7 = quotaInfoArray[];
//		protected long sellQty7 = quotaInfoArray[];
//		protected double sellPrice8 = quotaInfoArray[];
//		protected long sellQty8 = quotaInfoArray[];
//		protected double sellPrice9 = quotaInfoArray[];
//		protected long sellQty9 = quotaInfoArray[];
//		protected double sellPrice10 = quotaInfoArray[];
//		protected long sellQty10 = quotaInfoArray[];
		
		String unknow0 = quotaInfoArray[29];//请求时间
		quotation.setUnknown0(unknow0);
	    String requstTime = quotaInfoArray[30];//请求时间
	    quotation.setRequstTime(requstTime);
	    String risingfalling = quotaInfoArray[31];//涨跌
	    quotation.setRisingfalling(risingfalling);
	    String risingfallingPercent = quotaInfoArray[32];//涨跌%
	    quotation.setRisingfallingPercent(risingfallingPercent);
	    String highestPrice = quotaInfoArray[33];//最高
	    quotation.setHighestPrice(highestPrice);
	    String minimumPrice = quotaInfoArray[34];//最低
	    quotation.setMinimumPrice(minimumPrice);
	    String priceInfo = quotaInfoArray[35];//最新价/成交量（手）/成交额（元）
	    quotation.setPriceInfo(priceInfo);
	    String knockQty = quotaInfoArray[36];//成交量
	    quotation.setKnockQty(knockQty);
	    String knockAmt = quotaInfoArray[37];//成交额
	    quotation.setKnockAmt(knockAmt);
	    String turnoverRate = quotaInfoArray[38];//换手率
	    quotation.setTurnoverRate(turnoverRate);//
	    String PERatio = quotaInfoArray[39];//ttm市盈率
	    quotation.setPERatio(PERatio);
	    String unknown = quotaInfoArray[40];//未知
	    String highestPrice1 = quotaInfoArray[41];//最高
	    quotation.setHighestPrice1(highestPrice1);
	    String minimumPrice1 = quotaInfoArray[42];//最低
	    quotation.setMinimumPrice1(minimumPrice1);
	    String amplitude = quotaInfoArray[43];//振幅
	    quotation.setAmplitude(amplitude);
	    String circulationMarketValue = quotaInfoArray[44];//流通市值
	    quotation.setCirculationMarketValue(circulationMarketValue);
	    String stkValue = quotaInfoArray[45];//总市值
	    quotation.setStkValue(stkValue);
	    String priceToBookRatio = quotaInfoArray[46];//lf市净率
	    quotation.setPriceToBookRatio(priceToBookRatio);
	    String limitupprice = quotaInfoArray[47];//涨停价
	    quotation.setLimitupprice(limitupprice);
	    String limitdownprice = quotaInfoArray[48];//跌停价
	    quotation.setLimitdownprice(limitdownprice);
	    String volumeRatio = quotaInfoArray[49];//量比
	    quotation.setVolumeRatio(volumeRatio);
	    String volumeRatio1 = quotaInfoArray[50];//均价
	    quotation.setVolumeRatio1(volumeRatio1);
	    String averagePrice = quotaInfoArray[51];//动态市盈率
	    quotation.setAveragePrice(averagePrice);
	    String dynamicPERatio = quotaInfoArray[52];//静态市盈率
	    quotation.setDynamicPERatio(dynamicPERatio);
	    String staticPERatio = quotaInfoArray[53];//
	    quotation.setStaticPERatio(staticPERatio);
	    String unknown1 = quotaInfoArray[54];//未知
	    String unknown2 = quotaInfoArray[55];//未知
	    String unknown3 = quotaInfoArray[56];//未知
	    String Turnover = quotaInfoArray[57];//成交额
	    String unknown4 = quotaInfoArray[58];//未知
	    String unknown5 = quotaInfoArray[59];//未知
	    String unknown6 = quotaInfoArray[60];//未知
	    String unknown7 = quotaInfoArray[61];//未知
	    String unknown8 = quotaInfoArray[62];//未知
	    String unknown9 = quotaInfoArray[63];//未知
	    String unknown10 = quotaInfoArray[64];//未知
	    String unknown11 = quotaInfoArray[65];//未知
	    String unknown12 = quotaInfoArray[66];//未知
	    FastExLog.log(quotation.toString());	
		return quotation;
	}
	
	@Override
	public void run() {
			QuotationToRedisService quotaRedisService = QuotationToRedisService.getQuotationToRedisService();
			DbManagerDao dbManagerDao = new DbManagerDao();
			List<String> stkIds = PropertiesUtil.getPropKey("stkId");
			while(true){
				try{
					Thread.sleep(1000);
					for(String stkId : stkIds){
						String exchId = "";
						if(stkId.startsWith(TradeConst.hkExchId)){
							exchId = TradeConst.hkExchId;
						}
						if(!TradeTimeUtils.getInstance().isTradeTime(exchId)){
							FastExLog.log("非交易时间");
							continue;
						}
						//行情str转为对象
						Quotation quotation = getQuotaInfo(stkId);
						//放入java内存
						QuotationCacheManager quotationCache = QuotationCacheManager.getQuotationCacheInstance();
						quotationCache.setQuotation(quotation);
						//放入redis中
						quotaRedisService.WriteQuotationToRedis(quotation);
						//插入数据库中
						dbManagerDao.insertQuotation(DtoToBeanUtils.quotationToBean(quotation));
						
					}
				}catch(Exception e){
					FastExLog.log(e);
				}
			}
	}
	
	public static void main(String[] args) {
//		new FastExLog().start();
		
		QuotationThread quotation = new QuotationThread();
		quotation.run();
		
//		DbManagerDao dbManagerDao = new DbManagerDao();
//		List<QuotationBean> rs = dbManagerDao.queryQuotation();
//		for(QuotationBean quota : rs){
//			System.out.println(quota.toString());
//		}
	}
}

